package rpg.manager
{
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	import rpg.Utils.ByteArrayUtil;
	import rpg.Utils.Log;
	import rpg.data.FightState;
	import rpg.data.Spell;
	import rpg.data.SpellAnimation;
	import rpg.data.SpriteStat;
	import rpg.data.WorldMap;

	import ui.util.StringUtil;

	public class DataManager
	{
		/**
		 * 单列
		 */
		private static var instance : DataManager;
		/**
		 * 世界地图
		 */
		public var worldMapData : Vector.<WorldMap>;
		/**
		 * 特效所对应的swf
		 */
		public var spellsAnimation : Dictionary = new Dictionary();
		/**
		 * 状态列表
		 */
		public var globalbuffer : Dictionary = new Dictionary();
		/**
		 * 人物状态列表
		 */
		public var globalSpriteState : Dictionary = new Dictionary();
		/**
		 * 技能列表
		 */
		public var globalSpell : Dictionary = new Dictionary();

		public function DataManager()
		{

		}

		public static function getInstance() : DataManager
		{
			if(instance == null)
				instance = new DataManager();
			return instance;
		}

		/**
		 *解析总配置文件
		 * @param byteArray
		 *
		 */
		public function parseConfig(byteArray : ByteArray) : void
		{
			byteArray = ByteArrayUtil.decryptByteArray(byteArray);
			byteArray.uncompress();
			while(byteArray.bytesAvailable)
			{
				var fileName : String = byteArray.readUTF();
				var len : int = byteArray.readUnsignedInt();
				var fileBytes : ByteArray = new ByteArray();
				byteArray.readBytes(fileBytes , 0 , len);
				var data : *;
				var fileType : String = fileName.split(".")[1];
				if(fileType == 'txt' || fileType == 'xml')
					data = fileBytes.readUTFBytes(fileBytes.bytesAvailable);
				else
					data = fileBytes;
				parseTxtConfig(fileName , data);
			}
		}

		/**
		 *解析每一个分类配置
		 * @param fileName
		 * @param data
		 *
		 */
		private function parseTxtConfig(fileName : String , data : *) : void
		{
			//Log.print('解析文件：' + fileName);
			switch(fileName)
			{
				case "worldmap.txt":
					parseWorldMap(data);
					break;
				case 'animation.txt':
					parseEffectAnimation(data);
					break;
				case 'states.txt':
					parseStatesTxt(data);
					break;
				case 'SpriteStat.txt':
					parseSpriteStatTxt(data);
					break;
				case 'damage_spells.txt':
					parseSpellsTxt(data);
					break;
			}
		}

		/**
		 * 解析技能动作配置文件
		 * @param data
		 *
		 */
		public function parseEffectAnimation(value : *) : void
		{
			var descriptions : Array = parseText(value);
			//字段数组
			var keys : Array = descriptions.shift().split("\t");
			var rows : int = descriptions.length;
			for(var i : int = 0 ; i < rows ; i++)
			{
				var description : Array = String(descriptions[i]).split("\t");
				var spellAnimation : SpellAnimation = new SpellAnimation();
				for(var j : int = 0 ; j < keys.length ; j++)
				{
					spellAnimation.readProperty(keys[j] , description[j]);
				}
				spellsAnimation[spellAnimation.name] = spellAnimation;
			}
		}

		/**
		 *解析世界地图配置文件
		 * @param data
		 *
		 */
		private function parseWorldMap(data : *) : void
		{
			var descriptions : Array = parseText(data);
			worldMapData = new Vector.<WorldMap>();
			//字段数组
			var rows : int = descriptions.length;
			for(var i : int = 0 ; i < rows ; i++)
			{
				var tmp : String = descriptions[i];
				var description : Array = String(descriptions[i]).split("\t");
				var wordMap : WorldMap = new WorldMap(description);
				worldMapData.push(wordMap);
			}
		}

		/**
		 *根据场景id获取配置
		 * @param mapId
		 * @return
		 *
		 */
		public function getWorldMapById(mapId : int) : WorldMap
		{
			var len : int = worldMapData.length;
			for(var i : int = 0 ; i < len ; i++)
			{
				var wordMap : WorldMap = worldMapData[i];
				if(wordMap.mapId == mapId)
					return wordMap;
			}
			return null;
		}

		/**
		 *根据场景名称获取配置
		 * @param mapName
		 * @return
		 *
		 */
		public function getWorldMapByName(mapName : String) : WorldMap
		{
			var len : int = worldMapData.length;
			for(var i : int = 0 ; i < len ; i++)
			{
				var wordMap : WorldMap = worldMapData[i];
				if(wordMap.mapName == mapName)
					return wordMap;
			}
			return null;
		}

		/**
		 * 解析状态表
		 * @param res
		 * @param spellType
		 */
		public function parseStatesTxt(stateStr : String) : void
		{
			var descriptions : Array = parseText(stateStr);
			//字段数组
			var keys : Array = descriptions.shift().split("\t");
			var rows : int = descriptions.length;
			for(var i : int = 0 ; i < rows ; i++)
			{
				if(String(descriptions[i]) == "")
					continue;
				var description : Array = String(descriptions[i]).split("\t");
				var state : FightState = new FightState();
				for(var j : int = 0 ; j < keys.length ; j++)
				{
					state.readProperty(keys[j] , description[j]);
				}
				globalbuffer[state.id] = state;
			}
		}


		/**
		 * 解析人物的状态表
		 * @param res
		 *
		 */
		public function parseSpriteStatTxt(value : String) : void
		{
			var descriptions : Array = parseText(value);
			//字段数组
			var keys : Array = descriptions.shift().split("\t");
			var rows : int = descriptions.length;
			for(var i : int = 0 ; i < rows ; i++)
			{
				var description : Array = String(descriptions[i]).split("\t");
				var stat : SpriteStat = new SpriteStat();
				for(var j : int = 0 ; j < keys.length ; j++)
				{
					stat.readProperty(keys[j] , description[j]);
				}
				globalSpriteState[stat.id] = stat;
			}
		}


		/**
		 * 解析技能配置文件
		 * @param spllStr
		 *
		 */
		public function parseSpellsTxt(spllStr : String) : void
		{
			var descriptions : Array = parseText(spllStr);
			//字段数组
			var keys : Array = descriptions.shift().split("\t");
			var rows : int = descriptions.length;
			for(var i : int = 0 ; i < rows ; i++)
			{
				var description : Array = String(descriptions[i]).split("\t");
				var spell : Spell = new Spell();
				for(var j : int = 0 ; j < keys.length ; j++)
				{
					spell.readProperty(keys[j] , description[j]);
				}
				globalSpell[spell.id] = spell;
			}
		}

		public function parseText(data : String) : Array
		{
			var descriptions : Array = data.split("\r\n");
			if(descriptions && descriptions.length > 0 && !descriptions[descriptions.length - 1])
				descriptions.pop(); //去掉最后的空白
			descriptions.shift(); //去掉第一行描述
			return descriptions;
		}

	}
}